package com.zhx.real.security.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * describe: 无权限操作的情况下弹出的提示
 *
 * @author zhanghaixuan
 * @date 2018/08/21
 **/
@Component("myAccessDeniedHandler")
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    private  final static Logger logger = LoggerFactory.getLogger(MyAccessDeniedHandler.class);

    @Bean
    @ConditionalOnMissingBean(name = "cuestomAccessDeniedHandler")
    public CuestomAccessDeniedHandler cuestomAccessDeniedHandler(){
        return new DefaultCuestomAccessDeniedHandler();
    }

    @Autowired
    private CuestomAccessDeniedHandler cuestomAccessDeniedHandler;


    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        cuestomAccessDeniedHandler.handle(request,response,accessDeniedException);
    }
}
